package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.SimsGrade;
import com.sims.service.IGradeService;
import com.sims.service.impl.GradeServiceImp;

public class GradePageAction extends ActionSupport {
	private String gridId;
	private SimsGrade grade = null;
	private IGradeService gradeService = new GradeServiceImp();

	public String getGridId() {
		return gridId;
	}

	public void setGridId(String gridId) {
		this.gridId = gridId;
	}

	public SimsGrade getGrade() {
		return grade;
	}

	public void setGrade(SimsGrade grade) {
		this.grade = grade;
	}

	// -----------分页获取数据---------------
	private int pageNo = 1;
	private int pageSize = 4;
	private int pageCount;
	private long rowCount;

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	// 获取所有账号
	public String listGrade() throws Exception {
		System.out.println("-----listGradePage");
		rowCount = gradeService.getRowCount();
		// 获取总页数
		if (rowCount % pageSize == 0) {
			pageCount = (int) (rowCount / pageSize);
		} else {
			pageCount = (int) (rowCount / pageSize) + 1;
		}
		if (pageNo < 1) {
			pageNo = 1;
		} else if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		// 分页获取记录
		List<SimsGrade> list = gradeService.getAllSimsGrade(pageNo, pageSize);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("GradeList", list);
		return "list";
	}

	// 删除账号
	public String deleteGrade() throws Exception {
		System.out.println("-----deleteGrade id=" + gridId);
		Long id = Long.parseLong(gridId);
		boolean bFlag = gradeService.deletSimsGradeById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listGrade();
	}

	// 修改账号
	public String editGrade() throws Exception {
		System.out.println("-----editGrade id=" + gridId);
		Long id = Long.parseLong(gridId);
		grade = gradeService.getSimsGradeById(id);
		if (grade == null) {
			// 未获取到用户对象给提示
			this.addActionError("id=" + id + ",未获取到用户对象！");
		}
		return "edit";
	}

	// 编辑账号
	@InputConfig(resultName = "edit_input")
	public String updateGrade() throws Exception {
		System.out.println("-----updateGrade");
		boolean bFlag = gradeService.updateSimsGrade(grade);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError(grade.getGradeName() + ",您填写的年级名称已存在！请重新填写！");
			return "edit";
		}
	}

	// 编辑账号Action校验方法
	public void validateUpdateGrade() throws Exception {
		System.out.println("----validateUpdateGrade");
		if (grade == null) {
			// 未获取到年级对象给提示
			this.addActionError("未获取到年级对象！");
		} else {
			if (grade == null || grade.getGradeCode() == null
					|| grade.getGradeCode().trim().length() <= 0) {
				this.addFieldError("gradeCodeInfo", "请输入年级代码 ！");
			}
			if (grade == null || grade.getGradeName() == null
					|| grade.getGradeName().trim().length() <= 0) {
				this.addFieldError("gradeNameInfo", "请输入年级名称！");
			}
			if (grade == null || grade.getDepartment() == null
					|| grade.getDepartment().trim().length() <= 0) {
				this.addFieldError("departmentInfo", "请填写系部！");
			}
			if (grade == null || grade.getMajor() == null
					|| grade.getMajor().trim().length() <= 0) {
				this.addFieldError("majorInfo", "请填写专业 ！");
			}
		}
	}

	@InputConfig(resultName = "create_input")
	public String createGrade() throws Exception {
		System.out.println("----createGrade---");
		IGradeService gradeService = new GradeServiceImp();
		Long id2 = gradeService.createGrade(grade);
		if (id2 > 0) {
			// 新建成功
			this.addActionError(grade.getGradeName() + ",新建成功！");
			return listGrade();
		} else {
			// 新建失败
			this.addActionError(grade.getGradeName() + ",您填写的年级名称已存在！请重新填写！");
			return "create";
		}
	}

	// 创建Action校验方法
	public void validateCreateGrade() throws Exception {
		System.out.println("----validateCreateGrade---");
		if (grade == null) {
			// 未获取到年级对象给提示
			this.addActionError("未获取到年级对象！");
		} else {
			if (grade == null || grade.getGradeCode() == null
					|| grade.getGradeCode().trim().length() <= 0) {
				this.addFieldError("gradeCodeInfo", "请输入年级代码 ！");
			}
			if (grade == null || grade.getGradeName() == null
					|| grade.getGradeName().trim().length() <= 0) {
				this.addFieldError("gradeNameInfo", "请输入年级名称！");
			}
			if (grade == null || grade.getDepartment() == null
					|| grade.getDepartment().trim().length() <= 0) {
				this.addFieldError("departmentInfo", "请填写系部！");
			}
			if (grade == null || grade.getMajor() == null
					|| grade.getMajor().trim().length() <= 0) {
				this.addFieldError("majorInfo", "请填写专业 ！");
			}
		}
	}
}
